import { Router } from 'express';
import { UrlEntity } from '../../framework/baseUrl';
import { Phrases, Translations, Word,  WordType } from '../dao/WorkDao';
import { Result } from 'spen-common/src/Result';
import {WordEntity} from 'spen-common/src/entity/WordEntity'
import {PageQuery} from 'spen-common/src/Query';
const router = Router()

router.get("/word", async (req, res)=>{

    let query: PageQuery = new PageQuery(<any>req.query);
    let word: WordEntity[] = <any>await Word.findAll({
        offset: query.getOffset(),
        limit: query.getLimit(),
        where: {
            typeId: query['type'] == undefined ? '1' : query['type']
        },
        include: query['word'] == 'only' ? [Translations] : [Phrases, Translations]
    });
    res.send(<Result<any>>{
        code: 'ok',
        data: <WordEntity[]>word
    });
})

router.get('/directory/',async (req, res)=>{
    let wordType = await WordType.findAll();
    if(wordType != null){
        res.send(<Result<any>>{
            code: 'ok',
            data: wordType
        })
    }
})

export default <UrlEntity>{
    type: 'router',
    application: router
}